package com.imooc.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;
import java.util.Date;

/**
 * Created by zghgchao 2018/4/15 15:25
 * Filter只能拿到HTTP的request和respond，作用与URL路径下的所有方法，不能具体到方法
 */
//@Component
public class TimeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("my time filter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("my time filter start");
        long start = new Date().getTime();
        filterChain.doFilter(servletRequest,servletResponse);//执行下一个过滤器链
        System.out.println("time filter 耗时: "+ (new Date().getTime()-start));
        System.out.println("my time filter finish");
    }

    @Override
    public void destroy() {
        System.out.println("my time filter destroy");
    }
}
